package com.cloud.thelen.wallpaper.AllAdapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.IntRange;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseRecyclerHolder> {
    protected FragmentActivity activity;
    private int layoutRes;
    protected List<T> items;

    public BaseRecyclerAdapter(FragmentActivity activity, @LayoutRes int layoutRes) {
        this.activity = activity;
        this.layoutRes = layoutRes;
        this.items = new ArrayList<>();
    }

    public BaseRecyclerAdapter(FragmentActivity activity, @LayoutRes int layoutResId, @Nullable List<T> data) {
        this.activity = activity;
        this.items = data;
        if (layoutResId != 0) {
            this.layoutRes = layoutResId;
        }
    }

    @NonNull
    @Override
    public BaseRecyclerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BaseRecyclerHolder(LayoutInflater.from(activity).inflate(layoutRes, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull BaseRecyclerHolder holder, int position) {
        T data = items.get(position);
        convert(holder, data);
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public void setData(List<T> items) {
        this.items = items;
        notifyDataSetChanged();
    }

    public ArrayList<T> getData() {
        ArrayList arrayList = (ArrayList) items;
        return arrayList;
    }

    public void addData(@IntRange(from = 0) int position, @NonNull T data) {
        items.add(position, data);
        notifyItemInserted(position);
        compatibilityDataSizeChanged(1);
    }

    public void addData(@NonNull List<T> data) {
        items.addAll(data);
        notifyDataSetChanged();
        notifyItemInserted(items.size());
    }

    public void addData(@NonNull T data) {
        items.add(data);
        notifyDataSetChanged();
        notifyItemInserted(items.size());
    }

    public void clear() {
        items.clear();
        notifyDataSetChanged();
    }


    public void remove(int position) {
        items.remove(position);
        int internalPosition = position;
        notifyItemRemoved(internalPosition);
        notifyItemRangeChanged(internalPosition, items.size() - internalPosition);
    }

    public void updateData(@IntRange(from = 0) int position, @NonNull T data) {
        items.set(position, data);
        notifyDataSetChanged();
    }

    private void compatibilityDataSizeChanged(int size) {
        final int dataSize = items == null ? 0 : items.size();
        if (dataSize == size) {
            notifyDataSetChanged();
        }
    }

    protected abstract void convert(BaseRecyclerHolder holder, T data);

}
